package com.indexingsystem.boss.utils;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

import org.apache.commons.fileupload.disk.DiskFileItem;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.indexingsystem.boss.common.Constants;

public class FileUtils {

	/**
	 * MultipartFile 转换成File
	 * 
	 * @param multfile
	 *            原文件类型
	 * @return File
	 * @throws IOException
	 */
	private static File multipartToFile(MultipartFile multfile) throws IOException {
		String suffix = multfile.getOriginalFilename().substring(multfile.getOriginalFilename().lastIndexOf("."));
					
		CommonsMultipartFile cf = (CommonsMultipartFile) multfile;
		// 这个myfile是MultipartFile的
		DiskFileItem fi = (DiskFileItem) cf.getFileItem();
		File file = fi.getStoreLocation();
		// 手动创建临时文件
		if (file.length() < Constants.MIN_FILE_SIZE) {
			File tmpFile = new File(System.getProperty("java.io.tmpdir") + System.getProperty("file.separator") + file.getName());			
			String fileName = UUID.randomUUID().toString()+ suffix; 
			
			multfile.transferTo(tmpFile);
			return tmpFile;
		}
		return file;
	}

}
